﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Serialization;
using System.Xml;
using NASA.BeAMartian.Controls;
using System.IO;
using System.Text.RegularExpressions;
using NASA.BeAMartian.Controls.News;

namespace NASA.BeAMartian.Views.Learn
{
    public partial class Article : PhoneApplicationPage
    {
        public Article()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Article_Loaded);
        }

        void Article_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient xmlClient = new WebClient();
            xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
            xmlClient.DownloadStringAsync(new Uri(String.Format("http://mars.jpl.nasa.gov/rss/newsitem.cfm?id={0}", this.NavigationContext.QueryString["id"]), UriKind.Absolute));
        }

        void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XmlSerializer x = new XmlSerializer(typeof(NewsItems));
                NewsItem ni;

                using (XmlReader reader = XmlReader.Create(new StringReader(e.Result)))
                {
                    ni = ((NewsItems)x.Deserialize(reader))[0];
                }

                ApplicationTitle.Text = ni.Title;
                DataContext = ni;
                wbArticleText.NavigateToString(GetArticleText(ni.NewsText));
                //tbNewsText.Text = GetArticleText(ni.NewsText);
            }
        }

        string GetArticleText(string str)
        {
            try
            {
                if (str.Contains("[[LINK"))
                {
                    //Parse links
                    do
                    {
                        int start = str.IndexOf("[[LINK");
                        int stop = str.IndexOf("||]]");
                        string linkRaw = str.Substring(start, stop + 4 - start);
                        string link = str.Substring(start + 1, stop - start - 1).Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries)[1];

                        link = String.Format("<a href=\"{0}\">{0}</a>", link);

                        str = str.Replace(linkRaw, link);

                    } while (str.Contains("[[LINK"));
                }

                //Remove Image boxes
                str = Regex.Replace(str, @"\[\[.+?\]\]", "");

                ////Remove HTML tags
                //str = Regex.Replace(str, "<.*?>", "");

                str = str.Replace("\n", "<br />");
            }
            catch
            {
                //just return what we have...
            }

            return str;
        }
    }
}